package com.leesche.zcsp.node.web.controller.web.v2.param;

import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.model.PubParamGroup;
import com.leesche.zcsp.node.web.service.PubParamGroupService;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubParamGroupCondition;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

/**
 * Created by dennies yang on 2017-01-05.
 * web端参数组管理
 */
@Controller
@RequestMapping("/v2/paramGroup")
public class WebParamGroupController {
    private final static Logger LOGGER = LoggerFactory.getLogger(WebParamGroupController.class);

    private static  final String PARAMGROUP_MAINVIEW="";

    @Autowired
    PrivilegeService privilegeService;

    @Autowired
    PubParamGroupService pubParamGroupService;


    /***
     * <p>参数组主界面显示</[p>
     * @param model
     * @param condition
     * @return
     */
    @RequestMapping(value = "/showView", method = RequestMethod.GET)
    public String showView(Model model,QueryPubParamGroupCondition condition
    ) {
        return PARAMGROUP_MAINVIEW;
    }

    @ResponseBody
    @RequestMapping(value = "addParamGroup", method = RequestMethod.POST)
    public Map<String,Object> addParamGroup(PubParamGroup paramGroup){
        if(paramGroup.getDevicetypeid()==null){
            return WebResult.failure("请选择设备类型!").toMap();
        }
        if(StringUtils.isEmpty(paramGroup.getParamgroupname())){
            return WebResult.failure("参数组名称不能为空!").toMap();
        }
        paramGroup.setCreatetime(new Date());
        paramGroup.setUpdatetime(new Date());
        paramGroup.setAdminid(privilegeService.getCurrentPrincipal().getUserid());

        try{
            pubParamGroupService.insert(paramGroup);
            return WebResult.success().info("操作成功!").toMap();
        }catch (Exception e){
            LOGGER.error("PubParamGroup isnert",e);
            return WebResult.failure(e).toMap();
        }
    }


    @ResponseBody
    @RequestMapping(value = "editParamGroup", method = RequestMethod.POST)
    public Map<String,Object> editParamGroup(PubParamGroup paramGroup){
        PubParamGroup pubParamGroup=pubParamGroupService.selectByPrimaryKey(paramGroup.getParamgroupid());
        if(pubParamGroup.getAdminid()!=null&&!pubParamGroup.getAdminid().equals(privilegeService.getCurrentPrincipal().getUserid())){
            return WebResult.failure("您无权限操作此参数组").toMap();
        }
        if(paramGroup.getDevicetypeid()==null){
            return WebResult.failure("请选择设备类型!").toMap();
        }
        if(StringUtils.isEmpty(paramGroup.getParamgroupname())){
            return WebResult.failure("参数组名称不能为空!").toMap();
        }
        paramGroup.setCreatetime(new Date());
        paramGroup.setUpdatetime(new Date());
        paramGroup.setAdminid(privilegeService.getCurrentPrincipal().getUserid());

        try{
            pubParamGroupService.updateByPrimaryKey(paramGroup);
            return WebResult.success().info("操作成功!").toMap();
        }catch (Exception e){
            LOGGER.error("PubParamGroup isnert",e);
            return WebResult.failure(e).toMap();
        }
    }

    @ResponseBody
    @RequestMapping(value = "deleteParamGroup", method = RequestMethod.POST)
    public Map<String,Object> deleteParamGroup(Long paramGroupId){
        PubParamGroup pubParamGroup=pubParamGroupService.selectByPrimaryKey(paramGroupId);
        if(pubParamGroup.getAdminid()!=null&&!pubParamGroup.getAdminid().equals(privilegeService.getCurrentPrincipal().getUserid())){
            return WebResult.failure("您无权限操作此参数组").toMap();
        }
        try{
            pubParamGroupService.deleteByPrimaryKey(paramGroupId);
            return WebResult.success().info("操作成功!").toMap();
        }catch (Exception e){
            LOGGER.error("PubParamGroup isnert",e);
            return WebResult.failure(e).toMap();
        }
    }


}
